﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;

namespace QLCV
{
    public partial class frmSuaMatKhau : Form
    {
        public frmSuaMatKhau()
        {
            InitializeComponent();
        }               
        
        private void frmSuaMatKhau_Load(object sender, EventArgs e)
        {
            btnDongY.Enabled = false;
        }

        private void btnHuy_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void txtMatKhauCu_TextChanged(object sender, EventArgs e)
        {
            if (txtMatKhauCu.Text.Trim() != "" && txtMatKhauMoi.Text.Trim() != "" && txtXacNhan.Text.Trim() != "")
                btnDongY.Enabled = true;
            else
                btnDongY.Enabled = false;
        }

        private void txtMatKhauMoi_TextChanged(object sender, EventArgs e)
        {
            if (txtMatKhauCu.Text.Trim() != "" && txtMatKhauMoi.Text.Trim() != "" && txtXacNhan.Text.Trim() != "")
                btnDongY.Enabled = true;
            else
                btnDongY.Enabled = false;
        }

        private void txtXacNhan_TextChanged(object sender, EventArgs e)
        {
            if (txtMatKhauCu.Text.Trim() != "" && txtMatKhauMoi.Text.Trim() != "" && txtXacNhan.Text.Trim() != "")
                btnDongY.Enabled = true;
            else
                btnDongY.Enabled = false;
        }

        private void btnDongY_Click(object sender, EventArgs e)
        {
            TaiKhoanDTO entity = new TaiKhoanDTO();
            entity.Username = GolbalSettings.CurrentUser.Username;
            entity.Password = GolbalSettings.Md5(txtMatKhauCu.Text.Trim());
            try
            {
                if (TaiKhoanBUS.Check(entity))
                {
                    if (txtMatKhauMoi.Text.Trim() != txtXacNhan.Text.Trim())
                    {
                        DialogResult retry = MessageBox.Show("Xác nhận không trùng khớp với Mật Khẩu mới.", "Error", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        if (retry == DialogResult.Retry)
                        {
                            txtXacNhan.Focus();
                            txtXacNhan.SelectAll();
                        }
                        else
                        {
                            this.Close();
                        }
                    }
                    else
                    {
                        entity.Password = GolbalSettings.Md5(txtMatKhauMoi.Text.Trim());
                        if (TaiKhoanBUS.Change(entity))
                        {
                            MessageBox.Show("Thay đổi mật khẩu thành công.", "Da doi mat khau", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);
                            this.Close();
                        }
                        else
                        {
                            MessageBox.Show("Không thể thay đổi mật khẩu.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                        }
                    }
                }
                else
                {
                    DialogResult retry = MessageBox.Show("Sai mật khẩu.", "Access denied", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
                    if (retry == DialogResult.Retry)
                    {
                        txtMatKhauCu.Focus();
                        txtMatKhauCu.SelectAll();
                    }
                    else
                    {
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }            
        }               
    }
}
